#ifndef LIBTORRENT_DELEGATOR_H
#define LIBTORRENT_DELEGATOR_H

#include "piece.h"
#include "bitfield.h"
#include "bitfield_counter.h"
#include <list>
#include <string>

namespace torrent {

class Delegator {
public:
	typedef enum {
		NONE,
		DOWNLOADING,
		FINISHED
	} PieceState;

	struct PieceInfo {
		PieceInfo(Piece p) : m_piece(p), m_state(NONE) {}

		Piece m_piece;
		PieceState m_state;
		std::string m_id;	// Id doing the downloading.
	};

	// Internal, not to be confused with the chunk.h
	struct Chunk {
		Chunk(int index) : m_index(index) {}

		int m_index;
		std::list<PieceInfo> m_pieces;
	};

	typedef std::list<Chunk> Chunks;

	Delegator() : m_bitfield(NULL) {}
	Delegator(const BitField* bf, unsigned int chunkSize, unsigned int lastSize) :
		m_bitfield(bf),
		m_chunkSize(chunkSize),
		m_lastSize(lastSize),
		m_bfCounter(bf->sizeBits()) {}

	~Delegator();

	BitFieldCounter& bfCounter() { return m_bfCounter; }

	Chunks& chunks() { return m_chunks; }

private:
	const BitField* m_bitfield;
	unsigned int m_chunkSize;
	unsigned int m_lastSize;

	Chunks m_chunks;
	BitFieldCounter m_bfCounter;
};

} // namespace torrent

#endif /* LIBTORRENT_DELEGATOR_H */
